package com.css.bjdt_report.executive.report;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.css.bjdt_report.cell.entity.Cell;
import com.css.bjdt_report.cell.entity.Cell.CellType;
/**
 * 
 * @author qiushi
 *
 */
public class ReportCache {
	private static ReportCache instance;
	//以报表id为key<以单元格类型为key>
	private Map<String,Map<String,List<Cell>>> cache = new ConcurrentHashMap<String,Map<String,List<Cell>>>();
	
	private ReportCache(){
	}
	
	public static synchronized ReportCache getInstance(){
		if(instance == null){
			instance = new ReportCache();
		}
		return instance;
	}
	
	public void putContent(String templateId, CellType type, List<Cell> cells){
		String typeKey = type.getKey();
		if(!cache.containsKey(templateId)){
			cache.put(templateId, new HashMap<String,List<Cell>>());
		}
		Map<String,List<Cell>> map = cache.get(templateId);
		if(!map.containsKey(typeKey)){
			map.put(typeKey, new ArrayList<Cell>());
		}
		List<Cell> list = map.get(typeKey);
		list.addAll(cells);
	}
	
	public Map<String,List<Cell>> getContent(String templateId){
		return cache.get(templateId);
	}
	
	public List<Cell> getContentByType(String templateId, CellType type){
		if(cache.containsKey(templateId)){
			if(cache.get(templateId).containsKey(type.getKey())){
				return cache.get(templateId).get(type.getKey());
			}
		}
		return null;
	}
	
	public void clear(){
		cache.clear();
	}
	
}
